package com.lianzhihui.romeex.widget.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.inter.DialogListener;
import com.lianzhihui.romeex.ui.base.BaseDialog;
import com.lianzhihui.romeex.utils.NumberUtils;
import com.lianzhihui.romeex.utils.UiUtils;
import com.lianzhihui.romeex.utils.UserUtils;

import java.text.MessageFormat;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description: 密码验证通用 弹窗
 * Author: RAMON
 * CreateDate: 2020/9/10 10:06 AM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class ZhuanzhangDialog extends BaseDialog {

    @BindView(R.id.tv_title)
    TextView titleTv;
    @BindView(R.id.title_trans)
    TextView title_trans;
    @BindView(R.id.warning)
    TextView warning;
    @BindView(R.id.tv_sum)
    TextView tv_sum;
    @BindView(R.id.input)
    EditText input;
    @BindView(R.id.tv_eyes)
    ImageView tv_eyes;
    @BindView(R.id.bt_confirm)
    Button bt_confirm;
    String address;
    DialogListener listener;
    String title;

    public ZhuanzhangDialog(@NonNull Activity activity) {
        super(activity);
//        this.title = activity.getResources().getString(R.string.input_password);
        this.title = "输入密码";
    }

    public void setListener(DialogListener listener) {
        this.listener = listener;
    }

    @Override
    protected int provideViewId() {
        return R.layout.dialog_zhuanzhang;
    }

    @SuppressLint("SetTextI18n")
    public void setSum(double sumD, String unit) {
        tv_sum.setText(NumberUtils.big8(sumD) + " " + unit);
    }

    @SuppressLint("SetTextI18n")
    public void setSum(String sumD, String unit) {
        tv_sum.setText(sumD + " " + unit);
    }

    @Override
    protected void initView() {


        input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                warning.setVisibility(View.INVISIBLE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void setTitle(String title) {
        titleTv.setText(title);
    }

    public void setTitle_trans(String title) {
        title_trans.setText(MessageFormat.format("{0}:", title));
    }

    @OnClick({R.id.bt_confirm, R.id.tv_eyes})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_confirm:

                String password = input.getText().toString();
                WalletInfo walletInfo;
                if (TextUtils.isEmpty(address)) {
                    //当前用户
                    walletInfo = UserUtils.getCurrentUser();
                } else {
                    //地址指定 用户
                    walletInfo = UserUtils.getUser(address);
                }

                if (walletInfo != null) {

                    String saftyCode = walletInfo.getSaftyCode();
                    if (TextUtils.isEmpty(saftyCode)) {
                        Toast.makeText(context, "用户信息错误", Toast.LENGTH_SHORT).show();
                    } else {
                        if (TextUtils.isEmpty(password)) {
                            warning.setVisibility(View.VISIBLE);
                        } else {
                            if (walletInfo.equSafeCode(password)) {
                                dismiss();
                                listener.onPassed();
                            } else {
                                listener.onFail();
                            }
                        }
                    }
                }

                break;
            case R.id.tv_eyes:
                UiUtils.setTextEyes(tv_eyes, input);
                break;
            default:
                break;
        }
    }

}
